<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="预订id" prop="bookingId">
        <el-input
          v-model="queryParams.bookingId"
          placeholder="请输入预订id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="酒店id" prop="hotelId">
        <el-input
          v-model="queryParams.hotelId"
          placeholder="请输入酒店id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入住日期">
        <el-date-picker
          v-model="daterangeCheckinDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="退房日期">
        <el-date-picker
          v-model="daterangeEndDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="人数" prop="guestCount">
        <el-input
          v-model="queryParams.guestCount"
          placeholder="请输入人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="需求房间数量" prop="roomCount">
        <el-input
          v-model="queryParams.roomCount"
          placeholder="请输入需求房间数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总价" prop="totalPrice">
        <el-input
          v-model="queryParams.totalPrice"
          placeholder="请输入总价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付状态" prop="paymentStatus">
        <el-select v-model="queryParams.paymentStatus" placeholder="请选择支付状态" clearable>
          <el-option
            v-for="dict in dict.type.payment_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['booking:book:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['booking:book:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['booking:book:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="预订id" align="center" prop="bookingId" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="酒店id" align="center" prop="hotelId" />
      <el-table-column label="入住日期" align="center" prop="checkinDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkinDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退房日期" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="人数" align="center" prop="guestCount" />
      <el-table-column label="需求房间数量" align="center" prop="roomCount" />
      <el-table-column label="总价" align="center" prop="totalPrice" />
      <el-table-column label="支付状态" align="center" prop="paymentStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.payment_status" :value="scope.row.paymentStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.paymentStatus !== '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['booking:book:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.paymentStatus !== '1'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['booking:book:remove']"
          >删除</el-button>
          <el-button
            v-if="scope.row.paymentStatus !== '1'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handlePay(scope.row)"
            v-hasPermi="['booking:book:pay']"
          >支付</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="updatePay" width="500px" append-to-body>
      <p>{{ sentence }}</p>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPay">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改酒店预订对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" :disabled=true />
        </el-form-item>
        <el-form-item label="酒店id" prop="hotelId">
          <el-input v-model="form.hotelId" placeholder="请输入酒店id" />
        </el-form-item>
        <el-form-item label="入住日期" prop="checkinDate">
          <el-date-picker clearable
            v-model="form.checkinDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入住日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退房日期" prop="endDate">
          <el-date-picker clearable
            v-model="form.endDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择退房日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="人数" prop="guestCount">
          <el-input v-model="form.guestCount" placeholder="请输入人数" />
        </el-form-item>
        <el-form-item label="需求房间数量" prop="roomCount">
          <el-input v-model="form.roomCount" placeholder="请输入需求房间数量" />
        </el-form-item>
        <el-form-item label="总价" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入总价" :disabled=true />
        </el-form-item>
        <el-form-item label="支付状态" prop="paymentStatus">
          <el-radio-group v-model="form.paymentStatus" :disabled="true">
            <el-radio
              v-for="dict in dict.type.payment_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBook, getBook, delBook, addBook, updateBook } from "@/api/booking/book";


export default {
  name: "Book",
  dicts: ['payment_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 酒店预订表格数据
      bookList: [],
      // 弹出层标题
      title: "",
      sentence: '',
      // 是否显示弹出层
      open: false,
      updatePay : false,
      // 支付状态时间范围
      daterangeCheckinDate: [],
      // 支付状态时间范围
      daterangeEndDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookingId: null,
        userId: null,
        hotelId: null,
        checkinDate: null,
        endDate: null,
        guestCount: null,
        roomCount: null,
        totalPrice: null,
        paymentStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        hotelId: [
          { required: true, message: "酒店id不能为空", trigger: "blur" }
        ],
        checkinDate: [
          { required: true, message: "入住日期不能为空", trigger: "blur" }
        ],
        endDate: [
          { required: true, message: "退房日期不能为空", trigger: "blur" }
        ],
        guestCount: [
          { required: true, message: "人数不能为空", trigger: "blur" }
        ],
        roomCount: [
          { required: true, message: "需求房间数量不能为空", trigger: "blur" }
        ],
        totalPrice: [
          { required: true, message: "总价不能为空", trigger: "blur" }
        ],
        paymentStatus: [
          { required: true, message: "支付状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询酒店预订列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCheckinDate && '' != this.daterangeCheckinDate) {
        this.queryParams.params["beginCheckinDate"] = this.daterangeCheckinDate[0];
        this.queryParams.params["endCheckinDate"] = this.daterangeCheckinDate[1];
      }
      if (null != this.daterangeEndDate && '' != this.daterangeEndDate) {
        this.queryParams.params["beginEndDate"] = this.daterangeEndDate[0];
        this.queryParams.params["endEndDate"] = this.daterangeEndDate[1];
      }
      listBook(this.queryParams).then(response => {
        console.log(response.rows);
        this.bookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.updatePay = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bookingId: null,
        userId: null,
        hotelId: null,
        checkinDate: null,
        endDate: null,
        guestCount: null,
        roomCount: null,
        totalPrice: null,
        paymentStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCheckinDate = [];
      this.daterangeEndDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bookingId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bookingId = row.bookingId || this.ids
      getBook(bookingId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改酒店预订";
      });
    },
    handlePay(row){
      this.form=row
      this.data=row.totalPrice;
      this.sentence = `支付后不可再修改订单信息，确认支付 ${this.data} ？`;
      this.updatePay = true;
      this.title = "支付界面";
    },
    submitPay() {
            this.form.paymentStatus=1;
            updateBook(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.updatePay = false;
              this.getList();
            });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bookingId != null) {
            updateBook(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBook(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bookingIds = row.bookingId || this.ids;
      this.$modal.confirm('是否确认删除酒店预订编号为"' + bookingIds + '"的数据项？').then(function() {
        return delBook(bookingIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('booking/book/export', {
        ...this.queryParams
      }, `book_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
